import xlwt
import xlrd
import xlutils.copy
class ExcelHandle():
    def __init__(self,filename,worktable):
        self.filename = filename
        self.worktable = worktable
        self.test_data = xlrd.open_workbook(self.filename, formatting_info=True)  # 保留原格式
        # self.sheet = self.test_data.sheet_by_index(0)
        self.sheet = self.test_data.sheet_by_index(self.worktable)
    def writeExcel(self,row,col,value):
        self.row = row
        self.col = col
        self.args = value
        style = xlwt.XFStyle()
        oldsheet = xlrd.open_workbook(self.filename, formatting_info=True)
        newsheet = xlutils.copy.copy(oldsheet)
        # newwrite = newsheet.get_sheet(0)
        newwrite = newsheet.get_sheet(self.worktable)
        newwrite.write(self.row, self.col, str(value), style)
        newsheet.save(self.filename)
    def readExcel(self, row,col):
        # data = eval(self.sheet.cell_value(row, 2)) # 字典格式取Excel第二列
        comlist = str(self.sheet.cell_value(row, col))
        return comlist

if __name__ == '__main__':
    t = ExcelHandle('bookname.xlsx',0)

    for i in range(2,15):
        for j in range(1, 10):
            X = t.readExcel(i,1)
            print(X)
            print("______________")
            t.writeExcel(i,j,"sssss")